xarray time series
Offifial examples
Examples
Montly mean
code:python
monthly_avg = ds.resample(time="1MS").mean()
daily anomaly
code:python
if not 'dayofyear' in da.coords:
da.coords'dayofyear'=xr.where((da.dayofyear>59) & (~da.time.dt.is_leap_year),da.dayofyear+1,da.dayofyear) da_clim=da.groupby('dayofyear').mean()
da_clim=da_climda_clim.dayofyear!=60.interp(dayofyear=range(1,367)) # Feb 29 is the mean of Feb 28 and Mar 1 da_anomaly=da.groupby("dayofyear")-da_clim
By monthly mean from daily data
code:python
# in python 3 , "/2" should be "//2"
def bimonthly_mean(ds, calendar='standard'):
labels=xr.DataArray((ds.time.to_index().month+1)/2,coords=[ds"time"],name="labels") bimonthly=ds.groupby(labels).mean("time")
return bimonthly
Calculating Seasonal long-time Averages (JFM, AMJ, JAS, OND) from Timeseries of Monthly Means
code:python
# define season
# in python 3, "/3" should be "//3"
season=xr.DataArray([seasonnameint(n) for n in (ds.time.to_index().month-1)/3],coords=[ds"time"],name="season") #for "MAM","JJA","SOD","DJF" ## season=xr.DataArray([seasonnameint(n) for n in (ds.time.to_index().month-3)//3],coords=[ds"time"],name="season") ##
month_length = ds.time.dt.days_in_month
#
# Calculate the weights by grouping by 'season'.
# Conversion to float type ('astype(float)') only necessary for Python 2.x
weights = month_length.groupby(season) / month_length.astype(float).groupby(season).sum()
# check of weights
weights.groupby(season).sum()
# calculate weighted mean
dsweighted=(ds*weights).groupby(season).sum(dim="time")
time series of each season from monthly values (share similar parts with above example)
code:python
df_season=df.where(season==cseason,drop=True)
year=xr.DataArray(df_season.time.to_index().year,coords=[df_season"time"],name="year") month_length_season=month_length.where(season==cseason,drop=True)
weights = month_length_season.groupby(year) / month_length_season.groupby(year).sum()
df_season_mean=(df_season*weights).groupby(year).sum("time")
add time dimension to 2d dataset
code:pyhton
import datetime as dt
td=dt.datetime(2019,1,1)
dataset.expand_dims(dim={"time":td}) shift time
code:python
U_shifted=U.copy()
Tips